import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_app/base/base_state.dart';
import 'package:flutter_app/base/base_stateful_widget.dart';
import 'package:flutter_app/base/net/lp_net.dart';
import 'package:flutter_app/base/net/test/test_request.dart';
import 'package:flutter_app/business/splash/splash_request.dart';
import 'package:flutter_app/main.dart';

class StartPage extends BaseStatefulWidget {
  @override
  _StartPageState createState() => _StartPageState();
}

class _StartPageState extends BaseState<StartPage> {
  var leftSec = 5;

  @override
  void initState() {
    super.initState();
    startCountdown();
  }

  @override
  Widget build(BuildContext context) {
    print("_StartPageState build");
    return Scaffold(
      body: Stack(
        children: [
          Container(
            child: ConstrainedBox(
              constraints: BoxConstraints.expand(),
              child: Image.network(
                "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=851178854,1709478981&fm=26&gp=0.jpg",
                fit: BoxFit.cover,
              ),
            ),
          ),
          Positioned(
              right: 20,
              top: 30,
              child: GestureDetector(
                child: DecoratedBox(
                  decoration: BoxDecoration(
                    color: Colors.grey
                  ),
                  child: Text(
                    "倒计时${leftSec}s",
                    style: TextStyle(color: Colors.white, fontSize: 15),
                  ),
                ),
                onTap: () {
                  leftSec = 5;
                  startCountdown();
                },
              ))
        ],
      ),
    );
  }

  Timer? timer;

  void startCountdown() {
    timer = new Timer.periodic(Duration(seconds: 1), (timer) {
      setState(() {
        leftSec--;
        if (leftSec <= 0) {
          cancelCountdown();
          popAndPush(MyHomePage());
        }
      });
    });
  }

  void cancelCountdown() {
    timer?.cancel();
    timer = null;
  }
}
